﻿using System;
using App.Entity;
using App.IServices;
using App.IRepository;
using System.Collections.Generic;
using App.Code;

/*!
* 文件描述：一切从简，只为了更懒！
*/
namespace App.Services
{
	public partial class Sys_ModuleButtonServices : BaseServices<Sys_ModuleButton> , ISys_ModuleButtonServices
	{
		ISys_ModuleButtonRepository _ISys_ModuleButtonRepository;
		public Sys_ModuleButtonServices(ISys_ModuleButtonRepository __ISys_ModuleButtonRepository)
		{
			this._ISys_ModuleButtonRepository = __ISys_ModuleButtonRepository;
			base._IBaseRepository = __ISys_ModuleButtonRepository;
		}

        public void DeleteForm(string keyValue)
        {
            if (_ISys_ModuleButtonRepository.QueryCount(t => t.ParentId.Equals(keyValue)) > 0)
            {
                throw new Exception("删除失败！操作的对象包含了下级数据。");
            }
            else
            {
                _ISys_ModuleButtonRepository.Delete(t => t.Id == keyValue);
            }
        }

        public Sys_ModuleButton GetForm(string keyValue)
        {
            return _ISys_ModuleButtonRepository.QuerySingle(c => c.Id == keyValue);
        }

        public List<Sys_ModuleButton> GetList(string moduleId = "", string keyWord = "")
        {
            var expression = ExtLinq.True<Sys_ModuleButton>();
            if (!string.IsNullOrEmpty(moduleId))
            {
                expression = expression.And(t => t.ModuleId == moduleId);
            }
            if (!string.IsNullOrEmpty(keyWord))
            {
                expression = expression.And(t => t.FullName.Contains(keyWord));
            }
            return _ISys_ModuleButtonRepository.QueryByWhere(expression, "SortCode");

        }

        public void SubmitCloneButton(string moduleId, string Ids)
        {
            string[] ArrayId = Ids.Split(',');
            var data = this.GetList();
            List<Sys_ModuleButton> entitys = new List<Sys_ModuleButton>();
            foreach (string item in ArrayId)
            {
                Sys_ModuleButton moduleButtonEntity = data.Find(t => t.Id == item);
                moduleButtonEntity.Id = Common.GuId();
                moduleButtonEntity.ModuleId = moduleId;
                entitys.Add(moduleButtonEntity);
            }
            _ISys_ModuleButtonRepository.InsertRange(entitys);
        }

        public void SubmitForm(Sys_ModuleButton moduleButtonEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                moduleButtonEntity.Modify(keyValue);
                _ISys_ModuleButtonRepository.Update(moduleButtonEntity);
            }
            else
            {
                moduleButtonEntity.Create();
                _ISys_ModuleButtonRepository.Insert(moduleButtonEntity);
            }
        }
    }
}